package org.opens.video;

import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.OpenCVFrameGrabber;

/**
 * 功能描述
 *
 * @author: zhangyuming
 */
public class CameraCapture {

    public static void main(String[] args) {
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0); // 0代表默认摄像头
        CanvasFrame canvas = new CanvasFrame("摄像头视频");

        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        canvas.setAlwaysOnTop(true);

        try {
            grabber.start();
            while (true) {
                Frame frame = grabber.grab();
                if (frame == null) {
                    System.out.println("没有抓取到帧");
                    break;
                }
                canvas.showImage(frame);
            }
        } catch (FrameGrabber.Exception e) {
            e.printStackTrace();
        } finally {
            try {
                grabber.stop();
            } catch (FrameGrabber.Exception e) {
                e.printStackTrace();
            }
        }
    }

}
